package com.ebt.m.proposal_v2.ui;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.fragment.LazyFragment;
import com.ebt.m.proposal_v2.dao.request.ParamGetRisks;
import com.ebt.m.proposal_v2.dao.response.ResponseBrand;
import com.ebt.m.proposal_v2.mvp.presenter.MainProductPresenter;

import butterknife.ButterKnife;

/**
 * 筛选品牌菜单
 *
 * @author archer.qi
 *         Created on 2017/5/23.
 */
public class BrandSelectFragment extends LazyFragment {

    private FragmentBrandMenu mFrgMenu;
    private FragmentProducts mFrgProducts;

    private ResponseBrand mBrandChecked; // 当前选中品牌
    private ParamGetRisks mArg;

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        View contentView = inflater.inflate(R.layout.fragment_brand_select, null);
        ButterKnife.bind(this, contentView);
        setContentView(contentView);
        initArguments();
        init();
        onDataSyncPost();
    }

    public void initArguments() {
        MainProductActivity act = (MainProductActivity) getActivity();
        if (act != null) {
            mArg = act.getArgument();
        }
    }

    private void init() {
        initFragments();
        int companyType = getCompanyType();
        if (companyType < 0) {
            return;
        }
        if (companyType == MainProductPresenter.COMPANY_TYPE_INS) {
            toggleFragment(1);
        } else {
            toggleFragment(0);
        }
    }

    private int getCompanyType() {
        if (AppContext.getCurrentUser() == null) {
            return -1;
        }
        if (AppContext.getCurrentUser().getCompanyInfo() == null) {
            return -1;
        }
        return AppContext.getCurrentUser().getCompanyInfo().getCategory();
    }

    private void initFragments() {
        if (mFrgMenu == null) {
            mFrgMenu = new FragmentBrandMenu();
            initMenuListener();

        }
        if (mFrgProducts == null) {
            mFrgProducts = FragmentProducts.getInstance(mArg);
        }
    }

    /**
     * 切换页面： 菜单 or 列表
     *
     * @param tag
     */
    private void toggleFragment(int tag) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        if (tag == 0) {
            //公司选择
            transaction.replace(R.id.root, mFrgMenu).commitAllowingStateLoss();
        }
        if (tag == 1) {
            //产品选择
            transaction.replace(R.id.root, mFrgProducts).commitAllowingStateLoss();
        }
    }

    /**
     * 选择品牌监听
     */
    private void initMenuListener() {
        mFrgMenu.setOnBrandSelectListener(data -> {
            mBrandChecked = data;
            ((MainProductActivity) getActivity()).updateBrandText(getBrandChecked());
            mFrgProducts.onMenuBrandChanged(data);
            toggleFragment(1);
        });
    }

    /**
     * 获取当前品牌名称
     *
     * @return
     */
    public String getBrandChecked() {
        if (mBrandChecked == null) {
            return "筛选品牌";
        }
        return mBrandChecked.brandShortName;
    }

    /**
     * 重置选中品牌｛点击常用产品时，需要重置为空｝
     */
    public void clearBrandChecked() {
        mBrandChecked = null;
        if (mFrgMenu != null) {
            mFrgMenu.clearBrandChecked();
        }
        toggleFragment(0);
    }

    /**
     * 打开品牌菜单
     */
    public void onOpenBrandMenu() {
        toggleFragment(0);
    }

    /**
     * 关闭品牌菜单
     */
    public void onCloseBrandMenu() {
        toggleFragment(1);
    }

}
